﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class DragUI : MonoBehaviour {

    public static DragUI Instance;

    private Vector3 sunPos;
    private Vector3 stormPos;
    private Vector3 cloudPos;
    private AudioSource audios;
    
    public Transform storm;
    public Transform sun;
    public Transform cloud;
    public GameObject weather;
    public GameObject StormReplace;
    public AudioClip CorrectPut;
    public AudioClip Pass;
   

    // Use this for initialization
    void Start ()
    {
        Instance = this;
        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();

        //初始位置
        stormPos = storm.localPosition;
        sunPos = sun.localPosition;
        cloudPos = cloud.localPosition;
        DragEventListener dragListener = gameObject.AddComponent<DragEventListener>();

        dragListener.BeginDrag += delegate (GameObject obj, PointerEventData eventData)
          {
             
          };
        dragListener.Drag += delegate (GameObject obj, PointerEventData eventData)
          {
              var rt = gameObject.GetComponent<RectTransform>();

              Vector3 globalMousePos;
              if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos))
              {
                  rt.position = globalMousePos;
              }

          };
        dragListener.EndDrag += delegate (GameObject obj, PointerEventData eventData)
          {       
              //回到原位
              if (gameObject.name == "Sun")
              {
                  this.transform.localPosition = sunPos;
              }
              else if (gameObject.name == "Cloud")
              {
                  this.transform.localPosition = cloudPos;
              }
              //天气图标移动条件
              if (CollisionTracker.Instance.isSunReady && CollisionTracker.Instance.stormMove==true)
              {
                  transform.localPosition = stormPos;
                  //音效
                  audios.clip = CorrectPut;
                  audios.Play();
                  audios.loop = false;

                  //播放第二段动画
                  weather.GetComponent<Animator>().SetTrigger("weather2");
                  StartCoroutine("LastIntroduce");
              }
              //else if (CollisionTracker.Instance.isCloudReady && CollisionTracker.Instance.stormMove == true)
              //{
              //    transform.localPosition = stormPos;
              //    StartCoroutine("NextLevel");
              //}

          };
    }

    IEnumerator LastIntroduce()
    {
        yield return new WaitForSeconds(1);
        //分数
        GameObject.Find("UI_Game").SendMessage("AddNum",1);
        //关卡解锁
        if (!LevelSingleton.GetInstance().levelData.ContainsKey(1))
        {
            LevelSingleton.GetInstance().levelData.Add(1, 1);
        }
        

        audios.clip = Pass;
        audios.Play();
        audios.loop = false;

        PlayerPrefs.SetInt("1",1);


        yield return new WaitForSeconds(1.5f);
        GameObject.Find("Canvas").SendMessage("Introduce1Last");
    }
	
	
}
